package com.CampusLife.controller;



import com.CampusLife.bean.Admin;
import com.CampusLife.service.IAdminService;
import com.CampusLife.until.Encrypt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private IAdminService iAdminService;
    /**
     * 跳转到后台登录页
     */
    @RequestMapping("/login")
    public String admin_login(){
        return "admin/login";
    }

    /**
     *
     * @param mname
     * @param password
     * 管理员登录验证
     */
    @RequestMapping("/login_verify")

    public  String login_verify(String mname,String password,HttpSession httpSession){
        //给页面传过来的密码加密
        String ps=Encrypt.MD5(password)+"zhj";
        //再次加密防止密码外泄
        String  ps1=Encrypt.MD5(ps);
       Admin admin=iAdminService.login(mname,ps1);
        //如果查询结果不为空，则证明该用户合法，否则该用户不合法
        if (admin!=null){
           httpSession.setAttribute("mname",admin.getMname());
           httpSession.setAttribute("mid",admin.getMid());
            String mname1=(String) httpSession.getAttribute("mname");
            int id=(int) httpSession.getAttribute("mid");
            System.out.println(id);
            System.out.println(mname1);
            return "admin/index";

        }else {
            return "redirect:/admin/login";
        }
    }

    /**
     * 页面传递过来用户密码password 修改过的密码password1  确认密码password2
     */
    @RequestMapping("/alter_ps")
    public  String alterps(String password, String password1,String password2, HttpSession httpSession)throws Exception {
        String n = Encrypt.MD5(password) + "zhj";
        String ps = Encrypt.MD5(n);
        String mname=(String) httpSession.getAttribute("mname");
        int mid=(int) httpSession.getAttribute("mid");
        Admin admin = iAdminService.login(mname, ps);
            if (admin!= null&&password1.equals(password2)) {
                    String m = Encrypt.MD5(password1) + "zhj";
                    String ps1 = Encrypt.MD5(m);
                    Admin admin1= new Admin();
                    System.out.println(ps1);
                    admin1.setPassword(ps1);
                    admin1.setMid(mid);
                    iAdminService.UpdatePassword(admin1);
                    return "admin/login";
                } else {
                    return "admin/index";
                }

         }

    /**
     * 跳转到前台首页
     */
    @RequestMapping("/home")
    public String home() {
        return "/home.jsp";
    }
    /**
     * 跳转到修改密码页面
     */
    @RequestMapping("/updatepassword")
   public String  updatepassword(){
        return "admin/password-updata";
   }
    /**
     * 跳转到后台首页面
     */
    @RequestMapping("index")
    public String index(){
        return "admin/index";
    }
    /**
     * 管理员退出登录
     */
    @RequestMapping("/out")
    public String loginout(HttpSession httpSession){
        httpSession.invalidate();
        return "admin/login";
    }
}
